const BASE_URL='http://139.224.164.161:9998/index.php'
const TIME_OUT=80000
import store from '@/store'

const myRequest=function({url,method,data,header={}}){
	uni.addInterceptor('request', {
	  success(args) {
	    var code =args.data.code
		var msg=args.data.msg
		if(code != 200){
			if( code== 40001){
				uni.showToast({
					title:'账号过期，请重新登录'
				})
				uni.navigateTo({
					url:'/pages/login/index'
				})
			}
			uni.showToast({ 
				title:msg //只显示错误时候的消息
			})
		}
	
	  }, 
	  fail(err) {
	    // console.log('interceptor-fail',err)
	  }, 
	  complete(res) {
	    // console.log('interceptor-complete',res)
	  }
	})
	if(store.state.user.token){
		
		header.Token=store.state.user.token
	}
	return new Promise((resolve,reject)=>{
			uni.request({
				url:BASE_URL+url,
				method,
				data,
				header,
				timeout:TIME_OUT,
				success(res) {
					resolve(res)
				},
				fail: (err) => {
					reject(err)
				}
			})
	})
}

export default myRequest